package c.e.b.h.e.k;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import c.e.a.b.g.g;
import c.e.b.h.e.b;
import c.e.b.m.e;
import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern;

/* compiled from: IdManager */
public class q0 implements r0 {
    public static final Pattern f = Pattern.compile("[^\\p{Alnum}]");
    public static final String g = Pattern.quote("/");
    public final s0 a;
    public final Context b;
    public final String c;
    public final e d;
    public String e;

    public q0(Context context, String str, e eVar) {
        if (str != null) {
            this.b = context;
            this.c = str;
            this.d = eVar;
            this.a = new s0();
            return;
        }
        throw new IllegalArgumentException("appIdentifier must not be null");
    }

    public final synchronized String a(String str, SharedPreferences sharedPreferences) {
        String uuid;
        uuid = UUID.randomUUID().toString();
        if (uuid == null) {
            uuid = null;
        } else {
            uuid = f.matcher(uuid).replaceAll("").toLowerCase(Locale.US);
        }
        b bVar = b.a;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Created new Crashlytics IID: ");
        stringBuilder.append(uuid);
        bVar.b(stringBuilder.toString());
        sharedPreferences.edit().putString("crashlytics.installation.id", uuid).putString("firebase.installation.id", str).apply();
        return uuid;
    }

    public synchronized String b() {
        b bVar = b.a;
        synchronized (this) {
            String str = this.e;
            if (str != null) {
                return str;
            }
            Object obj;
            SharedPreferences n = g.n(this.b);
            g id = this.d.getId();
            String string = n.getString("firebase.installation.id", null);
            try {
                obj = (String) a1.a(id);
            } catch (Exception e) {
                String str2 = "Failed to retrieve installation id";
                if (bVar.a(3)) {
                    Log.d("FirebaseCrashlytics", str2, e);
                }
                obj = string != null ? string : null;
            }
            String str3;
            if (string == null) {
                SharedPreferences sharedPreferences = this.b.getSharedPreferences("com.crashlytics.prefs", 0);
                String string2 = sharedPreferences.getString("crashlytics.installation.id", null);
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("No cached FID; legacy id is ");
                stringBuilder.append(string2);
                bVar.b(stringBuilder.toString());
                if (string2 == null) {
                    this.e = a(obj, n);
                } else {
                    this.e = string2;
                    d(string2, obj, n, sharedPreferences);
                }
                str3 = this.e;
                return str3;
            }
            if (string.equals(obj)) {
                this.e = n.getString("crashlytics.installation.id", null);
                StringBuilder stringBuilder2 = new StringBuilder();
                stringBuilder2.append("Found matching FID, using Crashlytics IID: ");
                stringBuilder2.append(this.e);
                bVar.b(stringBuilder2.toString());
                if (this.e == null) {
                    this.e = a(obj, n);
                }
            } else {
                this.e = a(obj, n);
            }
            str3 = this.e;
            return str3;
        }
    }

    public String c() {
        String installerPackageName;
        s0 s0Var = this.a;
        Context context = this.b;
        synchronized (s0Var) {
            if (s0Var.a == null) {
                installerPackageName = context.getPackageManager().getInstallerPackageName(context.getPackageName());
                if (installerPackageName == null) {
                    installerPackageName = "";
                }
                s0Var.a = installerPackageName;
            }
            installerPackageName = "".equals(s0Var.a) ? null : s0Var.a;
        }
        return installerPackageName;
    }

    public final synchronized void d(String str, String str2, SharedPreferences sharedPreferences, SharedPreferences sharedPreferences2) {
        b bVar = b.a;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Migrating legacy Crashlytics IID: ");
        stringBuilder.append(str);
        bVar.b(stringBuilder.toString());
        sharedPreferences.edit().putString("crashlytics.installation.id", str).putString("firebase.installation.id", str2).apply();
        sharedPreferences2.edit().remove("crashlytics.installation.id").remove("crashlytics.advertising.id").apply();
    }

    public final String e(String str) {
        return str.replaceAll(g, "");
    }
}
